<?php

namespace App\Admin\Actions\Grid\RowActions;

use App\Admin\Forms\AgreementAuditForm;
use App\Admin\Forms\DisPatchForm;
use App\Admin\Repositories\ProjectAgreement;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Widgets\Modal;
use Dcat\Admin\Grid\RowAction;
use Dcat\Admin\Widgets\Table;
use function Symfony\Component\HttpFoundation\getLanguages;

class AgreementAuditAction extends RowAction
{
    protected $title = '   <i class="feather icon-external-link text-custom"></i> <span class="text-custom" title="">审核信息</span> &emsp;';

    public function render()
    {
        // 实例化表单类并传递自定义参数
        return Modal::make()
            ->lg()
            ->title($this->title)
            ->body($this->table(['row' => $this->getRow()]))
            ->button($this->title);
    }

    protected function table($input)
    {
        $row = $input['row'] ?? null;
        $auditLog = $row['audit_log'] ? json_decode($row['audit_log'], true) : [];

        $data = [];
        if ($auditLog) {
            $flowMap = \App\Admin\Repositories\ProjectAgreement::getFlowMap();
            foreach ($auditLog as $log) {
                foreach ($log as $name => $value) {
                    $currGlobalStatus = $log['audit_global_status'];
                    switch ($name) {
                        case 'audit_global_status':
                            $value = admin_trans_field($flowMap[$value]);
                            break;
                        case 'audit_status': // 当前审批结果
                            $res = ProjectAgreement::getAuditStatus($flowMap[$currGlobalStatus]);
                            $value = $res[$value];
                            break;
                        case 'pay_status': // 支付状态
                            $res = ProjectAgreement::getPayStatus();
                            $value = $res[$value];
                            break;
                        case 'audit_fee': // 当前审批结果
                            $res = ProjectAgreement::getAuditStatus($flowMap[$currGlobalStatus]);
                            $value = $res[$value];
                            break;
                        default:
                            break;
                    }
                    $item = [
                        'name' => admin_trans_field($name) . ':',
                        'value' => $value
                    ];
                    $data[] = $item;
                }
                $data[] = ['name' => '', 'value' => ''];
            }
        }

        Admin::style('.table td{height:25px} ');

        return Table::make([], $data)->withBorder();
    }
}
